/**
 * Author@ Cheng Feitian
 * Date: 2021-4-4
 * Description: 电脑组装案例
 */

#include <iostream>
#include <string>

using namespace std;

class AbstractCPU
{
public:
    virtual void calculate() = 0;
};

class AbstractVideoCard
{
public:
    virtual void display() = 0;
};

class AbstractMemory
{
public:
    virtual void storage() = 0;
};

class Computer
{
private:
    //抽象类不能实例化，只能创建指针
    AbstractCPU *m_cpu;
    AbstractMemory *m_Memory;
    AbstractVideoCard *m_VCard;

public:
    Computer(AbstractCPU *cpu, AbstractMemory *memory, AbstractVideoCard *vCard)
    {
        m_cpu = cpu;
        m_Memory = memory;
        m_VCard = vCard;
    }

    void doWork()
    {
        m_cpu->calculate();
        m_Memory->storage();
        m_VCard->display();
    }

    ~Computer()
    {
        if (m_cpu != NULL)
        {
            delete m_cpu;
            m_cpu = NULL;
            cout << "CPU已经被注销！" << endl;
        }
        if (m_Memory != NULL)
        {
            delete m_Memory;
            m_Memory = NULL;
            cout << "Memory已经被注销！" << endl;
        }
        if (m_VCard != NULL)
        {
            delete m_VCard;
            m_VCard = NULL;
            cout << "VideoCard已经被注销！" << endl;
        }
    }
};

class IntelCPU : public AbstractCPU
{
public:
    virtual void calculate()
    {
        cout << "Intel's CPU is processing!" << endl;
    }
};

class IntelVideoCard : public AbstractVideoCard
{
public:
    virtual void display()
    {
        cout << "Intel's VideoCard is processing!" << endl;
    }
};

class IntelMemory : public AbstractMemory
{
public:
    virtual void storage()
    {
        cout << "Intel's memory is processing!" << endl;
    }
};

class LenovoCPU : public AbstractCPU
{
public:
    virtual void calculate()
    {
        cout << "Lenovo's CPU is processing!" << endl;
    }
};

class LenovoVideoCard : public AbstractVideoCard
{
public:
    virtual void display()
    {
        cout << "Lenovo's VideoCard is processing!" << endl;
    }
};

class LenovoMemory : public AbstractMemory
{
public:
    virtual void storage()
    {
        cout << "Lenovo's memory is processing!" << endl;
    }
};

void test01()
{
    // Computer computer(new IntelCPU, new IntelMemory, new IntelVideoCard);
    // computer.doWork();

    AbstractCPU *CPU_1 = new IntelCPU;
    AbstractMemory *memory_1 = new IntelMemory;
    AbstractVideoCard *vCard_1 = new IntelVideoCard;

    Computer *computer_1 = new Computer(CPU_1, memory_1, vCard_1);
    computer_1->doWork();
    delete computer_1;

    AbstractCPU *CPU_2 = new LenovoCPU;
    AbstractMemory *memory_2 = new LenovoMemory;
    AbstractVideoCard *vCard_2 = new LenovoVideoCard;

    Computer *computer_2 = new Computer(CPU_2, memory_2, vCard_2);
    computer_2->doWork();
    delete computer_2;
}

int main()
{
    test01();
    return 0;
}
